package study.corejava.innerclass;

import java.util.Date;
import java.util.GregorianCalendar;

class Employee implements Comparable<Employee>, Cloneable {
	
	public Employee(String n, double s, double b) {
		name = n;
		salary = s;
		bonus = b;
		hireDay = new Date();
	}
	
	//实现clone方法
	public Employee clone() throws CloneNotSupportedException {
		//call Object.clone()
		Employee cloned = (Employee) super.clone();

		// clone mutable fields
		cloned.hireDay = (Date) hireDay.clone();

		return cloned;
	}
	
	public String toString() {
		return "Employee[name=" + name + ",salary=" + salary + ",bonus="+bonus+",hireDay="
				+ hireDay + "]";
	}
	public String getName() {
		return name;
	}

	public double getSalary() {
		return salary;
	}

	public double getBonus() {
		return bonus;
	}

	public void raiseSalary(double byPercent) {
		double raise = salary * byPercent / 100;
		salary += raise;
	}

	public void calculateBonus(double byPercent) {
		double bonus = salary * 12 * byPercent;
		this.bonus += bonus;

	}

	/**
	 * Compares employees by salary
	 * @param other another Employee object
	 * @return a negative value if this employee has a lower salary than
	 * otherObject, 0 if the salaries are the same, a positive value otherwise
	 */
	public int compareTo(Employee other) {
		if (salary < other.salary)
			return -1;
		if (salary > other.salary)
			return 1;
		return 0;
	}
	/**
	 * Set the hire day to a given date. 
	 * @param year the year of the hire day
	 * @param month the month of the hire day
	 * @param day the day of the hire day
	 */
	public void setHireDay(int year, int month, int day) {
		Date newHireDay = new GregorianCalendar(year, month - 1, day).getTime();

		// Example of instance field mutation
		hireDay.setTime(newHireDay.getTime());
	}
	// public int compareTo(Employee other)
	// {
	//   if(bonus < other.bonus) return -1;
	//   if(bonus > other.bonus) return 1;
	//   return 0;
	//  }

	private String name;
	private double salary;
	private double bonus;
	private Date hireDay;
}
